var database = require('../../data/data.js');
Page({
  data: {
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    this.setData({
      stadiumName: options.stadiumName
    })
    database.stadiumId = options.stadiumId;
    if (options.type != null) {
      var that = this;
      wx.login({
        success(res) {
          var code = res.code;
          wx.request({
            url: database.url + "/api/V2/user/access_token",
            data: {
              clientId: "QqKTO5BTPN6REI26QAgq",
              clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
            },
            success(res) {
              database.accessToken = res.data.data.accessToken;
              wx.request({
                url: database.url + '/api/V2/stadium/stadium_share',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                }, 
                data: {
                  accessToken: database.accessToken,
                  stadiumId: database.stadiumId
                },
                success(res){
                  database.stadiumPhoto = res.data.data.stadiumPhoto
                  database.stadiumName = res.data.data.stadiumName
                }
              })
              wx.request({
                url: database.url + '/api/V2/integral/integral_openid',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  code: code,
                  accessToken: database.accessToken,
                  weChatId: "D0C62A3A0C1F4BBD9BA7A35351C685C7",
                  regionName: "other"
                },
                success(res) {
                  var sessoionKey = res.data.data.session_key;
                  wx.getUserInfo({
                    data: {
                      withCredentials: true,
                      lang: "zh_CN"
                    },
                    success(res) {
                      wx.request({
                        url: database.url + '/api/V2/user/we_chat_user',
                        method: "POST",
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        data: {
                          accessToken: database.accessToken,
                          weChatId: "D0C62A3A0C1F4BBD9BA7A35351C685C7",
                          encryptedData: res.encryptedData,
                          iv: res.iv,
                          sessionKey: sessoionKey,
                          weChatName: "场馆小程序"
                        },
                        success(res) {
                          database.unionid = res.data.data.unionId;
                          database.openid = res.data.data.openId;
                          wx.request({
                            url: database.url + '/api/V2/user/verify_wechat_small',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              unionid: database.unionid
                            },
                            success(res) {
                              if (res.data.data.verify == "yes") {
                                database.userId = res.data.data.id
                                database.login = true;
                              } else {
                                database.login = false;
                              }
                              that.setData({
                                url: database.url + "/front/wechat/stadium/details?stadiumId=" + options.stadiumId + "&unionId=" + database.unionid
                              })
                              wx.hideLoading();
                            }
                          })
                        }
                      })
                    }
                  })
                }
              })
            }
          })
        }
      })
    } else {
      wx.request({
        url: database.url + '/api/V2/stadium/stadium_share',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          stadiumId: database.stadiumId
        },
        success(res) {
          database.stadiumPhoto = res.data.data.stadiumPhoto
          database.stadiumName = res.data.data.stadiumName
        }
      })
      this.setData({
        url: database.url + "/front/wechat/stadium/details?stadiumId=" + options.stadiumId + "&unionId=" + database.unionid
      })
      wx.hideLoading();
    }
  },
  onShow() {
    if (database.payFlag) {
      wx.navigateBack({
        
      })
    }
  },
  onShareAppMessage: function () {
    var that = this;
    return {
      title: database.stadiumName+"在线预订",
      imageUrl: database.stadiumPhoto,
      path: "/pages/detail/detail?stadiumId=" + database.stadiumId + "&type=share&stadiumName=" + that.data.stadiumName,
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})